﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

using System.Data.SqlClient;
using MySql.Data.MySqlClient;

namespace JA.Negocios
{
    public static partial class ModelBO
    {

        public static CaracteristicaVO getCaracteristica(int idCaracteristica, string caracteristica)
        {
            CaracteristicaVO caracteristicaVO = null;
            List<CaracteristicaVO> listCaracteristicaVOs = getCaracteristicas(idCaracteristica, caracteristica);
            if (listCaracteristicaVOs != null && listCaracteristicaVOs.Count > 0)
            {
                caracteristicaVO = listCaracteristicaVOs[0];
            }
            return caracteristicaVO;
        }

        public static List<CaracteristicaVO> getCaracteristicas()
        {
            return getCaracteristicas(null,null);
        }

        public static List<CaracteristicaVO> getCaracteristicas(int? idCaracteristica, string caracteristica)
        {
            string where = "WHERE ";
            StringBuilder query = new StringBuilder();

            query.AppendLine("SELECT IdCaracteristica, Caracteristica, Cadastro, CadastradoPor, Edicao, EditadoPor ");
            query.AppendLine("FROM caracteristica ");

            if (idCaracteristica != null)
            {
                query.AppendLine(where + "IdCaracteristica=?IdCaracteristica ");
                where = "AND ";
            }
            if (caracteristica != null)
            {
                query.AppendLine(where + "Caracteristica LIKE ?Caracteristica ");
                where = "AND ";
            }

            query.AppendLine("ORDER BY Caracteristica ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            if (idCaracteristica != null) cmd.Parameters.AddWithValue("?IdCaracteristica", idCaracteristica);
            if (caracteristica != null) cmd.Parameters.AddWithValue("?Caracteristica", "%" + caracteristica + "%");

            MySqlDataReader reader = null;
            try
            {
                cmd.Connection.Open();

                reader = cmd.ExecuteReader();

                List<CaracteristicaVO> listCaracteristica = new List<CaracteristicaVO>();

                while (reader.Read())
                {
                    CaracteristicaVO caracteristicaVO = CaracteristicaVO.CreateCaracteristicaVO();
                    caracteristicaVO.idCaracteristica = reader.GetInt32(reader.GetOrdinal("IdCaracteristica"));
                    caracteristicaVO.caracteristica = reader.GetString(reader.GetOrdinal("Caracteristica"));
                    caracteristicaVO.cadastro = reader.GetDateTime(reader.GetOrdinal("Cadastro"));
                    caracteristicaVO.cadastradoPor = reader.GetString(reader.GetOrdinal("CadastradoPor"));
                    caracteristicaVO.edicao = reader.GetDateTime(reader.GetOrdinal("Edicao"));
                    caracteristicaVO.editadoPor = reader.GetString(reader.GetOrdinal("EditadoPor"));
                    listCaracteristica.Add(caracteristicaVO);
                }
                reader.Close();
                return listCaracteristica;
            }
            catch (Exception ex){ throw ex; }
            finally { cmd.Connection.Close(); }
        }

        public static void setCaracteristica(CaracteristicaVO caracteristicaVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("UPDATE caracteristica ");
            query.AppendLine("SET Caracteristica=?Caracteristica, Cadastro=?Cadastro, CadastradoPor=?CadastradoPor, Edicao=?Edicao, EditadoPor=?EditadoPor ");
            query.AppendLine("WHERE IdCaracteristica=?IdCaracteristica ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdCaracteristica", caracteristicaVO.idCaracteristica);
            cmd.Parameters.AddWithValue("?Caracteristica", caracteristicaVO.caracteristica);
            cmd.Parameters.AddWithValue("?Cadastro", caracteristicaVO.cadastro);
            cmd.Parameters.AddWithValue("?CadastradoPor", caracteristicaVO.cadastradoPor);
            cmd.Parameters.AddWithValue("?Edicao", caracteristicaVO.edicao);
            cmd.Parameters.AddWithValue("?EditadoPor", caracteristicaVO.editadoPor);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static long newCaracteristica(CaracteristicaVO caracteristicaVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("INSERT INTO caracteristica ");
            query.AppendLine("  ( Caracteristica, Cadastro, CadastradoPor, Edicao, EditadoPor ) ");
            query.AppendLine("VALUES (?Caracteristica, ?Cadastro, ?CadastradoPor, ?Edicao, ?EditadoPor ) ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?Caracteristica", caracteristicaVO.caracteristica);
            cmd.Parameters.AddWithValue("?Cadastro", caracteristicaVO.cadastro);
            cmd.Parameters.AddWithValue("?CadastradoPor", caracteristicaVO.cadastradoPor);
            cmd.Parameters.AddWithValue("?Edicao", caracteristicaVO.edicao);
            cmd.Parameters.AddWithValue("?EditadoPor", caracteristicaVO.editadoPor);

            try
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                return cmd.LastInsertedId;
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static void delCaracteristica(CaracteristicaVO caracteristicaVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("DELETE FROM caracteristica ");
            query.AppendLine("WHERE IdCaracteristica=?IdCaracteristica ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdCaracteristica", caracteristicaVO.idCaracteristica);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }
    }
}
